<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item label="类型：">
                    <el-select v-model="filters.category" placeholder="请选择">
                        <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="运算：">
                    <el-select v-model="filters.value" multiple collapse-tags placeholder="请选择">
                        <el-option v-for="item in options2" :disabled="item.disabled" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="题数：">
                    <el-input-number v-model="filters.number" controls-position="right" :step="50"></el-input-number>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="tapSubmit">生成题目</el-button>
                </el-form-item>
                <!-- <el-form-item>
                    <el-button type="success">生成word</el-button>
                </el-form-item> -->
            </el-form>
        </el-col>
    </section>
</template>

<script>
    import Bus from './bus.js'
    export default {
        name: 'tool',
        data() {
            return {
                filters: {
                    category: 0,
                    number: 50,
                    value: [0]
                },
                options1: [{
                    value: 0,
                    label: '10以内'
                }, {
                    value: 1,
                    label: '20以内'
                }, {
                    value: 2,
                    label: '100以内'
                }],
                
                options2: [{
                    value: 0,
                    label: '加法'
                },{
                    value: 1,
                    label: '减法'
                }],

            }
        },

        methods: {
            tapSubmit() {
                var vm = this;
                var Maximum = [10, 20]
                // console.log(Maximum[vm.filters.category])
                var randomInt = function (nMin, nMax) {
                    return Math.floor(Math.random() * (nMax - nMin) + nMin + 0.5);
                };
                var randomOpt = function () {
                    var arr = ['+', '-', '×', '÷'];
                    return arr[vm.filters.value[Math.round(Math.random() * (vm.filters.value.length - 1))]];
                }

                var generateQuestion = function (difficulty) {
                    // difficulty = difficulty ? difficulty : 1
                    var question = '',
                        questionL, questionR, opt
                    // console.log(difficulty)
                    // while (difficulty--) {
                    //     question += randomInt(1, Maximum[vm.filters.category]) + randomOpt();
                    // }
                    // question += randomInt(1, Maximum[vm.filters.category]);
                    questionL = randomInt(1, Maximum[vm.filters.category]);
                    questionR = randomInt(1, Maximum[vm.filters.category]);
                    opt = randomOpt()
                    while (opt === '-' && (questionL < questionR)) {
                        questionL = randomInt(1, Maximum[vm.filters.category]);
                        questionR = randomInt(1, Maximum[vm.filters.category]);
                    }
                    // console.log(questionL,questionR,Maximum[vm.filters.category],questionL + questionR >= Maximum[vm.filters.category])
                    while (opt === '+' && (questionL + questionR >= Maximum[vm.filters.category])) {
                        questionL = randomInt(1, Maximum[vm.filters.category]);
                        questionR = randomInt(1, Maximum[vm.filters.category]);
                    }
                    

                    // while (opt === '÷' && (questionL < questionR || questionL % questionR !== 0 )) {
                    //     // console.log(questionL,questionL > questionR)
                    //     questionL = randomInt(1, Maximum[vm.filters.category]);
                    //     questionR = randomInt(1, Maximum[vm.filters.category]);
                    // }

                    return questionL + opt + questionR;
                };

                var getAnswer = function (question) {
                    var answer = null;
                    try {
                        answer = eval(question);
                    } catch (e) {}
                    return answer === null ? 'error' : answer;
                };
                
                
                var getTesting = function () {
                    let arr = []
                    for (let i = 0; i < vm.filters.number; i++) {
                        var question = generateQuestion(),
                            answer = getAnswer(question);
                        arr.push(question + '=')
                    }
                    return arr
                }
                // console.log(getTesting())

                Bus.$emit('val', getTesting())
            }
        },
        mounted() {

        }
    }
</script>

<style scoped>

</style>